import { observable, action } from 'mobx';
import { message } from 'antd';
import { WeaForm, WeaTableNew } from 'comsMobx';
import * as API from '../../apis/performance/programAuditListApi'
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-待审核方案
 * jiansong 2019/6/11
 */
const { TableStore } = WeaTableNew;
export default class ProgramAuditListStore {

    @observable loading = true; //加载中
    @observable tableStore = new TableStore(); // new table
    @observable form = new WeaForm(); // nrew 一个form
    @observable condition = []; // 存储后台得到的form数据
    @observable showSearchAd = false; // 高级搜索面板显示

    @observable programtype = [//方案周期
        { key: "", showname: "" },
        { key: "1", showname: getLabel('19398', '月度') },
        { key: "2", showname: getLabel('17495', '季度') },
        { key: "3", showname: getLabel('20729', '半年') },
        { key: "4", showname: getLabel('17138', '年度') },
    ];

    @action("初始化操作")
    doInit = () => {
        this.form.resetForm();
        this.getCondition();
        this.getTableDatas();
    };

    @action("获得高级搜索表单数据")
    getCondition = () => {
        this.loading = true;
        API.programAuditListConditionApi().then(action(res => {
            if (res.api_status) { // 接口请求成功/失败处理
                this.condition = res.condition;
                this.form.initFormFields(res.condition); // 渲染高级搜索form表单
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    };

    @action("渲染table数据")
    getTableDatas = () => {
        this.loading = true;
        const formParams = this.form.getFormParams() || {};
        API.programAuditListApi(formParams).then(action(res => {
            if (res.api_status) {
                this.tableStore.getDatas(res.datas, 1);
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    };

    @action("切换高级搜索框")
    setShowSearchAd = bool => this.showSearchAd = bool;

    @action("高级搜索")
    advanceSearch = () => {
        this.getTableDatas();
        this.showSearchAd = false;
    };

    @action("类型改变")
    onProgramtypeChange = (v) => {
        this.form.updateFields({ programtype: v });
        this.getTableDatas();
    };

    @action("待审核方案审核-操作")
    programAuditOperation = (params, updateNum) => {
        const { operation } = params
        if (operation === "all_approve" || operation === "all_return") {
            const formParams = this.form.getFormParams() || {}
            params = {
                ...params,
                ...formParams,
            };
        };
        this.loading = true;
        API.programAuditOperationApi(params).then(action(res => {
            if (res.api_status) {
                if (!res.hasRight) {
                    message.error(getLabel('2012', '对不起，您暂时没有权限！'));
                } else {
                    if (res.changeNum == 0 && operation === "all_approve") {
                        message.warning(getLabel('508855', '当前查询条件不存在可以批准的考核方案'))
                    } else if (res.changeNum == 0 && operation === "all_return") {
                        message.warning(getLabel('508856', '当前查询条件不存在可以退回的考核方案'))
                    } else {
                        this.tableStore.getDatas(res.datas);
                        if(updateNum){
                            updateNum();
                        }
                        message.success(getLabel('30700', '操作成功'));
                    }
                }
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    };

}